    package com.ruoyi.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.project.domain.entity.ProjectPay;
import com.ruoyi.project.domain.entity.ProjectWarranty;
import com.ruoyi.project.domain.query.ProjectPayQuery;
import com.ruoyi.project.domain.query.ProjectWarrantyQuery;
import com.ruoyi.project.service.IProjectPayService;
import com.ruoyi.project.service.IProjectWarrantyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

    @RestController
    @RequestMapping("/project/warranty")
    @Api(tags = "附件信息", description = "附件信息")
    public class ProjectWarrantyController {


        @Resource
        private IProjectWarrantyService projectWarrantyService;

        @ApiOperation("分页")
        @GetMapping(value = "/list")
        public R getList(ProjectWarrantyQuery query) {
            List<ProjectWarranty> list = projectWarrantyService.getList(query);
            for (ProjectWarranty projectWarranty : list) {
                if (projectWarranty.getEndDate() != null) {
                    setWarrantyRemaining(projectWarranty);
                }
            }
            return R.ok(list);
        }



        @ApiOperation("新增")
        @PostMapping(value = "/save")
        public R add(@RequestBody ProjectWarranty project) throws IOException {
            return R.ok(projectWarrantyService.addInfo(project));
        }


        @ApiOperation("新增")
        @PostMapping(value = "/saveSale")
        public R saveSale(@RequestBody ProjectWarranty project) throws IOException {
            return R.ok(projectWarrantyService.addInfo(project));
        }


        @ApiOperation("修改")
        @PutMapping (value = "/update")
        public R update(@RequestBody ProjectWarranty project) {
            return R.ok(projectWarrantyService.updateInfo(project));
        }

        @ApiOperation("修改")
        @PutMapping (value = "/updateSale")
        public R updateWarranty(@RequestBody ProjectWarranty project) {
            //
            LambdaQueryWrapper<ProjectWarranty> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProjectWarranty::getProjectNo, project.getProjectNo());
            try {
                ProjectWarranty one = projectWarrantyService.getOne(queryWrapper);
                one.setWarranty(project.getWarranty());
                return R.ok(projectWarrantyService.updateInfo(one));
            } catch (RuntimeException e) {
                throw new RuntimeException("服务器错误！");
            }

        }



        @ApiOperation("删除")
        @DeleteMapping (value = "/{id}")
        public R delete(  @PathVariable("id") String id) {
            return R.ok(projectWarrantyService.deleteInfo(id));
        }



        @ApiOperation("详情")
        @GetMapping(value = "/details")
        public R<ProjectWarranty> getDetails(@RequestParam String id) {
            ProjectWarranty byId = projectWarrantyService.getById(id);
            if (byId != null && byId.getEndDate() != null) {
                setWarrantyRemaining(byId);
            }
            return R.ok(byId);
        }

        public void setWarrantyRemaining(ProjectWarranty project) {
            // 假设 project.getEndDate() 返回的是 Date 类型
            Date endDate = project.getEndDate();

            // 获取当前日期（不包含时间部分）
            LocalDate today = LocalDate.now();

            // 将截止日期转换为 LocalDate（忽略时间部分）
            LocalDate deadline = endDate.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();

            // 计算相差的天数
            long daysLeft = java.time.temporal.ChronoUnit.DAYS.between(today, deadline);

            System.out.println("距离截止日期还有 " + daysLeft + " 天");

            // 如果你需要判断是否已过期：
            if (daysLeft < 0) {
                System.out.println("⚠️ 截止日期已过");
            } else {
                System.out.println("✅ 还有 " + daysLeft + " 天");
            }
            project.setWarrantyRemaining((int) daysLeft);
        }


    }
